<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="misc.Configurations"%>
<%@page import="controller.PasswordProtectController"%>
<%@page import="data.File"%>
<%@page import="data.AuthorizationSession"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title><%=Configurations.title%>Password-protect the file</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<h1>Password-protect this file</h1>
<%
AuthorizationSession.checkAuthorizationToken(request,response,true);

if(request.getParameter("file")!=null)
{
    PasswordProtectController control = new PasswordProtectController();
    File this_file = control.retrieveFile(request.getParameter("file"));
    
    if(this_file!=null)
    {       
        if(this_file.isDeleted())
        {
            %>
            <div class="error">The file is deleted already!</div>
            <%
        }
        else if(this_file.isProtected())
        {
            %>
            <div class="error">The file is protected already!</div>
            <%
        }
        else
        {
            boolean success=false;
            if(request.getParameter("password1")!=null && request.getParameter("password2")!=null)
            {
                String pwd1=request.getParameter("password1").trim();
                String pwd2=request.getParameter("password2").trim();
                if(!control.validate(pwd1,pwd2))
                {
                    %>
                    <div class="error">The passwords are not matched or empty!</div>
                    <%  
                }
                else
                {
                    success=control.addPassword(this_file.getFileID(),pwd1);
                    %>
                    <div class="success">The file is protected with the password successfully!</div>
                    <%
                }
            }

            if(!success)
            {
            %>
            <form action="PasswordProtect.jsp?file=<%=this_file.getHashedCode()%>" method="post">
                <table border="0">
                    <tr>
                      <td>File name:</td><td><%=this_file.getName() %></td>
                    </tr>
                    <tr>
                      <td>File size:</td><td><%=File.convertToMB(this_file.getSize()) %></td>
                    </tr>
                    <tr>
                      <td>Password</td><td><input type="password" name="password1"/></td>
                    </tr>
                    <tr>
                      <td>Password<br/><small>(again)</small></td><td><input type="password" name="password2"/></td>
                    </tr>
                    <tr>
                      <td colspan="2"><small>Note: Please write down this password.<br/>It will be hashed and you cannot<br/>recover if you lost it.</small></td>
                    </tr>
                    <tr>
                      <td colspan="2" align="right"><input type="submit" value="Set up"/></td>
                    </tr>
                </table>
            </form>
            <%
            }
        }
    }
    else
    {
        %>
        <div class="error">The file doesn't exist!</div>
        <%
    }
}
%>
<form>
    <input type="button" value="Back to management" onclick="javascript:window.location = 'ManageFiles.jsp';"/>
</form>
</body>
</html>